package cn.wolfcode.crmspringboot.service.impl;

import cn.wolfcode.crmspringboot.domain.Employee;
import cn.wolfcode.crmspringboot.domain.UserInfo;
import cn.wolfcode.crmspringboot.mapper.UserInfoMapper;
import cn.wolfcode.crmspringboot.query.EmployeeQuery;
import cn.wolfcode.crmspringboot.query.QueryObject;
import cn.wolfcode.crmspringboot.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    public UserInfo getUserInfo(String phone) {
        return userInfoMapper.getUserInfo(phone);
    }

    public void saveUser(UserInfo user) {
        userInfoMapper.saveUser(user);
    }

    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<UserInfo> list = userInfoMapper.selectForList(qo);
        list.forEach(a->{
            if(a.getLearn() != 0) {
                a.setHour(new BigDecimal(a.getLearn())
                        .divide(new BigDecimal(60 * 60), 2,
                                BigDecimal.ROUND_HALF_UP).toString() + "小时");
            }else {
                a.setHour("0 小时");
            }
        });
        return new PageInfo(list);
    }

    public void updateUserlearn(UserInfo userInfo) {
        userInfoMapper.updateUserlearn(userInfo);
    }


    @Scheduled(cron = "0 0/5 * * * ? ")
    public void calculate(){
        List<UserInfo> list = userInfoMapper.getUserList();
        for (UserInfo currentUser : list) {
            currentUser.setLearn(currentUser.getLearn() + 60 * 5);
            this.updateUserlearn(currentUser);
        }

    }

    @Scheduled(cron = "0 0/3 * * * ? ")
    public void logoutTwo(){
        userInfoMapper.logout();
    }

    public void updateUserState(String phone, String offline) {
        userInfoMapper.updateUserState(phone,offline);
    }


    public Workbook exportXls() {
        //创建excel文件
        Workbook wb = new HSSFWorkbook();
        //创建sheet(一页纸)
        Sheet sheet = wb.createSheet("用户名单");
        //标题行
        Row row = sheet.createRow(0);
        //设置内容到单元格中
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("年龄");
        row.createCell(2).setCellValue("手机号");
        row.createCell(3).setCellValue("用户创建时间");
        //查询员工数据
        List<UserInfo> employees = userInfoMapper.selectAll();
        for (int i = 0; i < employees.size(); i++) {
            UserInfo employee = employees.get(i);
            //创建行(每个员工就是一行)
            row = sheet.createRow(i+1);
            //设置内容到单元格中
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getAge());
            row.createCell(2).setCellValue(employee.getPhone());
            row.createCell(3).setCellValue(employee.getStartTime());
        }
        return wb;
    }

    public void deleteUser(String userId) {
        userInfoMapper.deleteUser(userId);
    }

    public void disableOrEnable(String phone, String status) {
        userInfoMapper.disableOrEnable(phone,status);
    }
}
